﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using SLLottery.LotterySrv;
using SLLottery.Common;
using System.Collections.ObjectModel;
namespace SLLottery.LotteryView.SSQ.Reverse
{
    public partial class NSSQ : UserControl
    {
        private LotterySrv.LotteryServiceClient srv;
        private int playtype = 0;
        int playTypeRadioId = 0;
        public NSSQ()
        {
            InitializeComponent();
            srv = WcfProxy.GetProxy;
            srv.GetPlayTypeCompleted += new EventHandler<GetPlayTypeCompletedEventArgs>(srv_GetPlayTypeCompleted);
            srv.GetPlayTypeRadioCompleted += new EventHandler<GetPlayTypeRadioCompletedEventArgs>(srv_GetPlayTypeRadioCompleted);
            srv.GetPlayTypeNumCompleted += new EventHandler<GetPlayTypeNumCompletedEventArgs>(srv_GetPlayTypeNumCompleted);
            srv.GetCurrenIssuNoCompleted += new EventHandler<GetCurrenIssuNoCompletedEventArgs>(srv_GetCurrenIssuNoCompleted);
            srv.GetOddsCompleted += new EventHandler<GetOddsCompletedEventArgs>(srv_GetOddsCompleted);
            this.Loaded += new RoutedEventHandler(NSSQ_Loaded);
        }
        void srv_GetOddsCompleted(object sender, GetOddsCompletedEventArgs e)
        {
            App.Session[Constant.REVERSEODDS_SESSION] = e.Result.ToList();
            Controls.RateGrid rateGrid = ratePanel.Children[0] as Controls.RateGrid;
            rateGrid.DataSource = e.Result.ToList();
            rateGrid.BindData();
            rateGrid = ratePanel.Children[1] as Controls.RateGrid;
            rateGrid.DataSource = e.Result.ToList();
            rateGrid.BindData();
            if ((PlayType)playtype == PlayType.NSSQBlue)
                return;
            rateGrid = ratePanel.Children[2] as Controls.RateGrid;
            rateGrid.DataSource = e.Result.ToList();
            rateGrid.BindData();
            
        }
        void srv_GetPlayTypeCompleted(object sender, GetPlayTypeCompletedEventArgs e)
        {
            App.Session[Constant.NSSQPLAYTYPE_SESSION] = e.Result.ToList();
            if (!App.Session.ContainsKey(Constant.PLAYTYPERADIO_SESSION))
            {
                srv.GetPlayTypeRadioAsync();
            }
            else if (!App.Session.ContainsKey(Constant.PLAYTYPENUM_SESSION))
            {
                srv.GetPlayTypeNumAsync();
            }
            else
            {
                InitPage();
            }
        }
        void srv_GetPlayTypeRadioCompleted(object sender, GetPlayTypeRadioCompletedEventArgs e)
        {
            App.Session[Constant.PLAYTYPERADIO_SESSION] = e.Result.ToList();
            if (!App.Session.ContainsKey(Constant.PLAYTYPENUM_SESSION))
            {
                srv.GetPlayTypeNumAsync();
            }
            else
            {
                InitPage();
            }
        }
        void srv_GetPlayTypeNumCompleted(object sender, GetPlayTypeNumCompletedEventArgs e)
        {
            App.Session[Constant.PLAYTYPENUM_SESSION] = e.Result.ToList();
            InitPage();
        }
        void NSSQ_Loaded(object sender, RoutedEventArgs e)
        {
            if (!App.Session.ContainsKey(Constant.PLAYTYPE_SESSION)
|| App.Session[Constant.PLAYTYPE_SESSION] == null)
            {
               srv.GetPlayTypeAsync((int)LotteryType.NSSQ);
            }
            else if (!App.Session.ContainsKey(Constant.PLAYTYPERADIO_SESSION))
            {
                srv.GetPlayTypeRadioAsync();
            }
            else if (!App.Session.ContainsKey(Constant.PLAYTYPENUM_SESSION))
            {
                srv.GetPlayTypeNumAsync();
            }
            else
            {
                List<PlayTypeInfo> list = App.Session[Constant.PLAYTYPE_SESSION] as List<PlayTypeInfo>;
                list = list.Where(p => p.LotteryTypeID1 == (int)LotteryType.NSSQ).ToList();
                App.Session[Constant.NSSQPLAYTYPE_SESSION] = list;
                InitPage();
            }
                
        }
        Controls.RateGrid CreateRateGrid(int startNum, int colNum, Controls.NumPanel numPanel,Controls.TouZhuCtrl touzhu)
        {
            Controls.RateGrid rategrid = new Controls.RateGrid();
            rategrid.StartNum = startNum;
            rategrid.ColNumCount = colNum;
            rategrid.isSSQ = true;
            rategrid.numPanel = numPanel;
            rategrid.touzhu = touzhu;
            this.ratePanel.Children.Add(rategrid);
            DockPanel.SetDock(rategrid, Dock.Top);
            return rategrid;
        }
        /*
        void InitPage()
        {
            ratePanel.Children.Clear();
            CreateRateGrid(1, 8);
            CreateRateGrid(9, 8);
            XuanHaoPanel.Children.Clear();
            DockPanel dockPanel = new DockPanel();
            dockPanel.LastChildFill = true;
            dockPanel.Width = 658;

            Grid grid = new Grid();
            grid.RowDefinitions.Insert(0, new RowDefinition() { Height = new GridLength() });
            grid.RowDefinitions.Insert(1, new RowDefinition() { Height = new GridLength(25) });

            Controls.NumPanel numPanel = new Controls.NumPanel(8, 2);
            Controls.RadioButtonPanel rbPanel = new Controls.RadioButtonPanel();
            grid.Children.Add(numPanel);
            grid.Children.Add(rbPanel);
            numPanel.SetValue(Grid.RowProperty, 0);
            rbPanel.SetValue(Grid.RowProperty, 1);

            Controls.TouZhuCtrl touzhu = new Controls.TouZhuCtrl();
            touzhu.isShow = false;
            DockPanel.SetDock(grid, Dock.Top);
            dockPanel.Children.Add(grid);

            DockPanel.SetDock(touzhu, Dock.Top);
            dockPanel.Children.Add(touzhu);
            numPanel.Text = "蓝码";
            rbPanel.Text = "蓝码,红码";
            rbPanel.CreateElement();
            foreach (RadioButton rb in rbPanel.PlayTypePanel.Children)
            {
                //rb.Checked += new RoutedEventHandler(rb_Checked);
            }
            XuanHaoPanel.Children.Add(dockPanel);
        }
        */
        private void PlayTypeButton_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Controls.PlayTypeButton Btn = (Controls.PlayTypeButton)sender;
            Border border = Btn.border;
            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = new BitmapImage(new Uri("/SLLottery;component/Images/Btn_Bg_Org.png", UriKind.Relative));
            border.Background = imageBrush;
            foreach (Controls.PlayTypeButton btn in buttonPanel.Children)
            {
                if (!Btn.Equals(btn))
                {
                    Border b = btn.border;
                    imageBrush = new ImageBrush();
                    imageBrush.ImageSource = new BitmapImage(new Uri("/SLLottery;component/Images/Btn_Bg_Black.png", UriKind.Relative));
                    b.Background = imageBrush;
                }
            }
            Redirect(Btn);
        }
        void InitPage()
        {
            List<PlayTypeInfo> list = App.Session[Constant.NSSQPLAYTYPE_SESSION] as List<PlayTypeInfo>;
            if (list == null || list.Count == 0)
            {
                Messager.Show("逆向双色球模块暂时故障!");
                App.Current.RootVisual = new LoginPage();
                return;
            }
            foreach (PlayTypeInfo l in list)
            {
                Controls.PlayTypeButton btn = new Controls.PlayTypeButton();
                btn.Text = l.PlayTypeName;
                btn.Tag = (PlayType)l.PlayTypeID;
                btn.MouseLeftButtonDown += new MouseButtonEventHandler(PlayTypeButton_MouseLeftButtonDown);
                buttonPanel.Children.Add(btn);
                if (list.First() == l)
                    PlayTypeButton_MouseLeftButtonDown(btn, null);
            }
        }
        void Redirect(Controls.PlayTypeButton btn)
        {
            playtype = (int)((PlayType)btn.Tag);
            MainPage.playType = Convert.ToInt32(btn.Tag);
            ratePanel.Children.Clear();
            Controls.NumPanel numPanel = new Controls.NumPanel(8, 2);
            Controls.TouZhuCtrl touzhu = new Controls.TouZhuCtrl();
            MainPage.TouzhuControl = touzhu;
            Controls.RadioButtonPanel rbPanel = new Controls.RadioButtonPanel();
            List<Controls.RateGrid> rateGrids = new List<Controls.RateGrid>();
            rateGrids.Add(CreateRateGrid(1, 8, numPanel, touzhu));
            rateGrids.Add(CreateRateGrid(9, 8, numPanel, touzhu));
            MainPage.rateGrids = rateGrids;
            if (XuanHaoPanel.Children.Count > 0)
            {
                XuanHaoPanel.Children.Clear();
            }
            if (touzhu.Parent != null)
            {
                ((DockPanel)touzhu.Parent).Children.Clear();
            }
            DockPanel dockPanel = new DockPanel();
            dockPanel.LastChildFill = true;
            dockPanel.Width = 658;

            Grid grid = new Grid();
            grid.RowDefinitions.Insert(0, new RowDefinition() { Height = new GridLength() });
            grid.RowDefinitions.Insert(1, new RowDefinition() { Height = new GridLength(25) });

            
            grid.Children.Add(numPanel);
            grid.Children.Add(rbPanel);
            numPanel.SetValue(Grid.RowProperty, 0);
            rbPanel.SetValue(Grid.RowProperty, 1);

            //TouzhuControl = touzhu;
            touzhu.lotteryType = LotteryType.NSSQ;
            touzhu.PlayType = (int)PlayType.NSSQBlue;
            touzhu.numPanel = numPanel;
            touzhu.rbPanel = rbPanel;
            touzhu.isShow = false;
            touzhu.PlayTypeName = btn.Text;
            touzhu.btBonus.IsEnabled = false;
            DockPanel.SetDock(grid, Dock.Top);
            dockPanel.Children.Add(grid);
            DockPanel.SetDock(touzhu, Dock.Top);
            dockPanel.Children.Add(touzhu);
            XuanHaoPanel.Children.Add(dockPanel);

            List<PlayTypeRadioInfo> list_radio = App.Session[Constant.PLAYTYPERADIO_SESSION] as List<PlayTypeRadioInfo>;
            list_radio = (from l in list_radio where l.PlayTypeID1 == (int)((PlayType)btn.Tag) select l).ToList();
            List<PlayTypeNumInfo> list_num = App.Session[Constant.PLAYTYPENUM_SESSION] as List<PlayTypeNumInfo>;
            list_num = (from l in list_num where l.PlayTypeID1 == (int)((PlayType)btn.Tag) select l).ToList();
            string numPanelText = string.Empty;
            string rbPanelText = string.Empty;
            string rbPanelValue = string.Empty;
            foreach (PlayTypeRadioInfo p in list_radio)
            {
                if (p == list_radio.First())
                    MainPage.playTypeRadio = Convert.ToInt32(p.PlayTypeRadioID);
                rbPanelText += p.PlayTypeRadioName;
                rbPanelValue += p.PlayTypeRadioID.ToString();
                if (p != list_radio.Last())
                {
                    rbPanelValue += ",";
                    rbPanelText += ",";
                }
            }
            foreach (PlayTypeNumInfo p in list_num)
            {
                numPanelText += p.PlayTypeNumName;
                if (p != list_num.Last())
                    numPanelText += ",";
            }
            numPanel.Text = numPanelText;
            rbPanel.Text = rbPanelText;
            rbPanel.Value = rbPanelValue;
            if ((PlayType)btn.Tag == PlayType.NSSQRed)
            {
                ratePanel.Children.Clear();
                CreateRateGrid(1, 11, numPanel, touzhu);
                CreateRateGrid(12, 11, numPanel, touzhu);
                CreateRateGrid(23, 11, numPanel, touzhu);
                numPanel.ColNum=11;
                numPanel.RowNum=3;
            }
            rbPanel.CreateElement();
            numPanel.CreateElement();
            foreach (RadioButton rbtn in rbPanel.PlayTypePanel.Children)
            {
                rbtn.Checked += (s, e) =>
                {
                    RadioButton rb = s as RadioButton;
                    if (rb.IsChecked == false)
                        return;
                    playTypeRadioId = Convert.ToInt32(rb.Tag);
                    MainPage.playType = playtype;
                    MainPage.playTypeRadio = playTypeRadioId;
                    srv.GetCurrenIssuNoAsync((int)LotteryType.SSQ);
                };
            }
            //选择号码时改变号码个数===========
            List<Controls.NumButton> listNumButton = numPanel.GetNumButton();
            List<Button> listCmdButton = numPanel.GetCmdButton();
            foreach (Controls.NumButton nb in listNumButton)
            {
                nb.MouseLeftButtonDown += (s, e) =>
                {
                    string num = numPanel.GetChoosedNum();
                    if (num == string.Empty)
                    {
                        touzhu.lbBetCount.Text = "0";
                        return;
                    }
                    string[] nums = num.Split(',');
                    touzhu.lbBetCount.Text = nums.Length.ToString();
                };
            }
            foreach (Button nb in listCmdButton)
            {
                nb.Click += (s, e) =>
                {
                    string num = numPanel.GetChoosedNum();
                    if (num == string.Empty)
                    {
                        touzhu.lbBetCount.Text = "0";
                        return;
                    }
                    string[] nums = num.Split(',');
                    touzhu.lbBetCount.Text = nums.Length.ToString();
                };
            }
            //-===============
            foreach (RadioButton rb in rbPanel.PlayTypePanel.Children)
            {
                rb.Checked += (s, er) =>
                {
                    touzhu.NumBasket.Items.Clear();
                    numPanel.ClearChild();
                    numPanel.isCreated = false;
                    numPanel.CreateElement();
                };
            }
            playTypeRadioId = Convert.ToInt32(((RadioButton)rbPanel.GetChoosedRadioButton()).Tag);
            srv.GetCurrenIssuNoAsync((int)LotteryType.SSQ);
        }
        void srv_GetCurrenIssuNoCompleted(object sender, GetCurrenIssuNoCompletedEventArgs e)
        {
            if (e.Result == null)
            {
                return;
            }
            srv.GetOddsAsync(playtype, playTypeRadioId, e.Result.IssueNo);
        }
        /*
        void rb_Checked(object sender, RoutedEventArgs e)
        {
            DockPanel dockPanel = (DockPanel)XuanHaoPanel.Children[0];
            Grid _grid = (Grid)dockPanel.Children[0];
            Controls.NumPanel numPanel = (Controls.NumPanel)_grid.Children[0];
            Controls.RadioButtonPanel rbPanel = (Controls.RadioButtonPanel)_grid.Children[1];
            if (((RadioButton)sender).Content.ToString() == "蓝码")
            {
                numPanel = new Controls.NumPanel(8, 2);
                numPanel.Text = "蓝码";
                rbPanel = new Controls.RadioButtonPanel();
                rbPanel.ChoosedIndex = 0;
                rbPanel.Text = "蓝码,红码";
                ratePanel.Children.Clear();
                CreateRateGrid(1, 8);
                CreateRateGrid(9, 8);
            }
            else
            {
                numPanel = new Controls.NumPanel(11, 3);
                numPanel.Text = "红码";
                rbPanel = new Controls.RadioButtonPanel();
                rbPanel.ChoosedIndex = 1;
                rbPanel.Text = "蓝码,红码";
                ratePanel.Children.Clear();
                CreateRateGrid(1, 11);
                CreateRateGrid(12, 11);
                CreateRateGrid(23, 11);
            }
            numPanel.SetValue(Grid.RowProperty, 0);
            rbPanel.SetValue(Grid.RowProperty, 1);
            rbPanel.CreateElement();
            foreach (RadioButton rb in rbPanel.PlayTypePanel.Children)
            {
                rb.Checked += new RoutedEventHandler(rb_Checked);
            }
            _grid.Children[0] = numPanel;
            _grid.Children[1] = rbPanel;

        }
         */
    }
}


